<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/2
 * Time: 14:59
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\School;
use app\sports_admin\model\SchoolNotice as SchoolNoticeModel;
use think\Request;
use app\exception\SchoolNoticeException;
use app\sports_admin\vaildate\Notice as NoticeVaildate;
use app\sports_admin\service\Base as BaseService;
class SchoolNotice extends Base
{

    protected $beforeActionList = [
        'isResource'=>['only'=>'index,noticelist,noticeaddpage,noticeadd,noticedel,noticeupdatepage,noticeupdate']
    ];
    /**
     * 学校公告页
     */

    public function index()
    {
        $school = School::field("school_id,school_name")->select();
        return view("index",compact("school"));
    }

    /**
     * 学校公告列表
     */
    public function noticeList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','school']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $school = trim($params['school']);
        return SchoolNoticeModel::noticeList($page,$rows,$sort,$sortOrder,$searchName,$school);
    }

    //添加学校公告页面
    public function noticeAddPage()
    {
        $school = School::field("school_id,school_name")->select();
        return view("notice_page",compact("school"));
    }

   //添加学校公告
    public function noticeAdd(Request $request)
    {
        if($request->isAjax()){
            return $data = SchoolNoticeModel::noticeAdd();
        }
    }
    /**
     * 删除学校公告
     */
    public function noticeDel(Request $request)
    {
        if($request->isAjax()){
            (new NoticeVaildate)->go_check();
            $params = BaseService::getHttpRequestPrograms();
            $params = $params->param();
            $bool = SchoolNoticeModel::destroy($params['id']);
            if(!$bool){
                throw new SchoolNoticeException(['msg'=>'删除失败','errorCode'=>400]);
            }
            return json(['msg'=>'删除成功','code'=>200]);
        }
    }
    /**
     * 编辑学校公告页面
     */
    public function noticeUpdatePage()
    {
        $id = Request::instance()->only('id');
        $data = SchoolNoticeModel::where('id',$id['id'])->find();
        return view('detail',compact('data'));
    }
    /**
     * 更新学校公告
     */
    public function noticeUpdate(Request $request)
    {
        if($request->isAjax()){
            $data = SchoolNoticeModel::noticeUpdate();
            if(!$data){
                throw new SchoolNoticeException([
                    'msg'=>'内容信息修改失败',
                    'errorCode'=>400
                ]);
            }
            return json(['msg'=>'修改成功','code'=>200]);
        }
    }
}